import { Effect, Reducer } from 'umi';

import { queryCurrent } from '@/services/user';
import { message } from 'antd';

// export interface AttemptModel {

// }

export interface AttemptModelState {
  questionInfo?: {};
  standard?: {};
}

export interface AttemptModelType {
  namespace: 'attempt';
  state: AttemptModelState;
  effects: {
    fetchCurrent: Effect;
  };
  reducers: {
    saveInfo: Reducer<AttemptModelState>;
  };
}

const AttemptModel: AttemptModelType = {
  namespace: 'attempt',
  state: {
    questionInfo: {},
    standard: {},
  },

  effects: {
    *fetchCurrent({ payload }, { call, put }) {
      const { code, data, info } = yield call(queryCurrent, payload);

      if (code) {
        message.info(info);
        return;
      }

      // const { isLeader } = data
      // const authority: string = isLeader ? 'leader' : 'user'
      // setAuthority(authority);

      yield put({
        type: 'saveContent',
        payload: data,
      });
    },
  },

  reducers: {
    saveInfo(state, { payload }) {
      console.log('payload', payload);
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default AttemptModel;
